package cc.lj.internal.coupons.infrastructure.repository.mysql;

import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.core.model.BasePageHelper;
import cc.lj.internal.core.model.PageParam;
import cc.lj.internal.coupons.domain.martcap.repository.ICapBatchRepository;
import cc.lj.internal.coupons.infrastructure.dao.ICapBatchMapper;
import cc.lj.internal.coupons.infrastructure.excepion.CoupousExceptionEnum;
import cc.lj.internal.coupons.infrastructure.po.CapBatchPO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 封顶额活动批次表　mysql仓储实现
 * @author liangxifeng
 * @date 2021-12-11
 */
@Repository("capBatchMysqlRespository")
public class CapBatchMysqlRespository implements ICapBatchRepository,BasePageHelper<CapBatchPO> {
    @Autowired
    private ICapBatchMapper mapper;
    @Override
    public List list(CapBatchPO entityPO) {
        QueryWrapper<CapBatchPO> wrapper = new QueryWrapper<>(entityPO);
        List<CapBatchPO> entitys = mapper.selectList(wrapper);
        if(entitys == null)
        {
            throw new ServiceException(CoupousExceptionEnum.CAP_BATCH_LIST_NULL);
        }
        return entitys;
    }

    @Override
    public Integer insert(CapBatchPO entityPO) {
        int rows = mapper.insert(entityPO);
        if( rows != 1) {
            throw new ServiceException(CoupousExceptionEnum.INSERT_CAP_BATCH_FAILED);
        }
        return entityPO.getId();
    }

    @Override
    public Integer updateById(CapBatchPO entityPO) {
        Integer res = mapper.updateById(entityPO);
        if (res != 1)
        {
            throw new ServiceException(CoupousExceptionEnum.UPDATE_CAP_BATCH_FAILED);
        }
        return res;
    }

    @Override
    public CapBatchPO getById(Integer id) {
        CapBatchPO entityPO =  mapper.selectById(id);
        if(entityPO==null) {
            throw new ServiceException(CoupousExceptionEnum.CAP_BATCH_NOT_FOUND);
        }
        return entityPO;
    }

    @Override
    public PageInfo<CapBatchPO> selectPage(PageParam pageParam) {
        return this.page(pageParam);
    }


    @Override
    public List<CapBatchPO> getList(CapBatchPO CapBatchPO) {
        return this.list(CapBatchPO);
    }

    @Override
    public Integer deleteById(Integer id) {
        int res = mapper.deleteById(id);
        if(res != 1)
        {
            throw new ServiceException(CoupousExceptionEnum.DELETE_CAP_BATCH_FAILED);
        }
        return res;
    }
}
